<template>
	<view class="content">
		<scroll-view scroll-x class="tabs-content" enable-flex scroll-with-animation>
			<view class="order-nav">
				<view class="nav-item" v-for="(nav, index) in orderState" :key="index" @tap="onNav(nav, index)">
					<view class="item-title" :class="{ 'item-title-active': tabindex === index }">{{ nav.title }}</view>
					<view class="nav-line" :class="{ 'line-active': tabindex === index }"></view>
				</view>
			</view>
		</scroll-view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<view class="balance-list">
				<view class="balance-item" v-for="(item, index) in diamondList" :key="index">
					<view class="balance-left">
						<view class="balance-status">{{item.source_name}}</view>
						<view class="balance-time">{{item.created_at}}</view>
					</view>
					<view class="balance-right">
						{{item.type == 1? '+':''}}{{item.change_value}}
					</view>
				</view>
				<view class="line"></view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex';
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				tabindex: 0,
				orderState: [{
						id: 0,
						title: '全部',
					},
					{
						id: 1,
						title: '收入',
					},
					{
						id: 2,
						title: '支出',
					}
				],
				diamondList: [],
				diamondType: 0,
				page: this.page
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad() {
			this.getUserDiamondList();
		},
		methods: {
			onNav(nav, index) {
				if (this.tabindex !== index) {
					this.tabindex = index;
					this.diamondType = index;
					this.downCallback();
				}
			},
			// 获取收入支出明细
			getUserDiamondList() {
				let apiUrl = `/addons/yun_shop/api.php?route=plugin.love.Frontend.Modules.Love.Controllers.love-records.index`
				let params = {
					page: this.page,
					uuid: 0,
					mid: this.userInfo.uid,
					shop_id: this.diamondType,
					service_type: this.diamondType
				}
				this.$api.get(apiUrl, params).then(res => {
					console.log(res,222);
					this.diamondList = res.data.data;
				})
			},
			// 下拉刷新列表
			downCallback() {
				this.mescroll.resetUpScroll(); // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			async upCallback(page) {
				this.page = page.num;
				let apiUrl = `/addons/yun_shop/api.php?route=plugin.love.Frontend.Modules.Love.Controllers.love-records.index`
				let params = {
					page: this.page,
					uuid: 0,
					mid: this.userInfo.uid,
					shop_id: this.diamondType,
					service_type: this.diamondType
				}
				this.$api.get(apiUrl, params).then(res => {
					let len = res.data.data.length
					let total = res.data.total
					if (page.num == 1) {
						this.diamondList = []; //如果是第一页需手动置空列表
					}
					this.diamondList = this.diamondList.concat(res.data.data); //追加新数据
					this.mescroll.endBySize(len, total);
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	@import './group-list.scss';
</style>
